//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "client\fn_clientExecutePickup.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

/// IMPORTANT
/// I would put the picked up object info on a GameLogic using SetupDrop, that way, you keep the same principle, plus you can set infos on the GameLogic to keep track of the item.

private ["_object","_action","_parameters","_item_desc","_action_title","_pickup_msg","_shared_variable","_shared_variable_value","_pickup_code","_drop_action","_action_params","_action_priority"
		,"_action_condition","_player_objects","_array_index","_forindex","_object_index","_pickedup_objects","_message","_act_args","_action_display_timer"];
DEFAULT_PARAM(_object,TMF_EXECUTE_ACTION_OBJECT,objNull);
DEFAULT_PARAM(_caller,TMF_EXECUTE_ACTION_CALLER,objNull);
DEFAULT_PARAM(_act_id,TMF_EXECUTE_ACTION_ID,-1);
DEFAULT_PARAM(_arguments,TMF_EXECUTE_ACTION_ARGS,[]);
DEFAULT_PARAM(_action,TMF_EXECUTE_ACTION_ACTION,[]);

_action_id = _arguments select TMF_EXECUTE_ACTION_PARAM_ACTID;

if (!isnull _object) then {
	if (_action_id > 0) then {
		_instance_id = [_object] call FFUNC(getInstanceID);
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);

		_obj_var = format["mission_object_pickup_player_%1",_action_id];
		_player = OGETVAR(_object,_obj_var,_caller);

		_item_desc = [_parameters, "ITEM_DESC", ([_object] call FFUNC(displayName)),THIS_FILE] call FFUNC(hashGet);
		_pickup_msg = [_parameters, "PICKUP_MSG", [],THIS_FILE] call FFUNC(hashGet);
		_shared_variable = [_parameters, "SHARED_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_initial_status = false; //[_parameters, "SHARED_VAR_VAL", false,THIS_FILE] call FFUNC(hashGet);
		_action_title = [_parameters, "PICKUP_TITLE", "STR_TMF_PICKUP_DEFACTION",THIS_FILE] call FFUNC(hashGet);
		_pickup_code = [_parameters, "PICKUP_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_is_picked_up = !_initial_status;
		_shared_variable = _shared_variable call FFUNC(getStringVar);
		
		[_object,_action,"DROP_ACTION",-1] call FFUNC(actionSetLocal);
		if (_player == player) then {
			_message = format["Player Picked up %1 (%2) at position %3",typeof _object, _action_id, getPos _object];
			NETLOG(_message);
			
			_action_title = ["STR_TMF_PICKUP_DROPACTION",_item_desc] call FFUNC(translate);
			_act_args = [];
			_act_args set [TMF_EXECUTE_ACTION_PARAM_ACTID, _action_id];
			_act_args set [TMF_EXECUTE_ACTION_PARAM_FUNC, "Drop"];
			_act_args set [TMF_EXECUTE_ACTION_PARAM_TARGET, _object];
			_action_display_timer = QGVAR(action_display_timer);
			player setVariable [_action_display_timer,0];
			_condition = Format["(alive _target) && {vehicle player == player} && {(_target getVariable '%1' < time)}",_action_display_timer];
			_drop_action = player addAction [_action_title, QPATH(addAction.sqf),_act_args,-10,False,True,"",_condition];
			// We set the variable as local because it is dependent on the computer running it.
			[_object,_action,"DROP_ACTION",_drop_action] call FFUNC(actionSetLocal);
			TRACE_1("Added Drop Action",ARR_5(_drop_action,_action_title,typeof _object,_condition,_act_args));
			_message = format["Player added drop action %1 (%2) for object %3 (%4)",player, _drop_action, _object,_act_args];
			NETLOG(_message);
		};
		[_object,_action_id,_is_picked_up] spawn {
			private ["_object","_action_id","_obj_var_picked","_is_picked_up"];
			DEFAULT_PARAM(_object,0,objNull);
			DEFAULT_PARAM(_action_id,1,-1);
			DEFAULT_PARAM(_is_picked_up,2,false);
			_obj_var_picked = format["mission_object_pickedup_%1",_action_id];
			_object hideObject true;
			_object enableSimulation false;
			waituntil {
				sleep 5.12345;
				!((OGETVAR(_object,_obj_var_picked,_is_picked_up)) && _is_picked_up)
			};
			_object enableSimulation true;
			_object hideObject false;
		};
	};
};
TRACE("End");
TMFPROFILERSTOP;